package com.gd.coreservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gd.coreservice.entity.Collection;
import com.gd.coreservice.entity.vo.CollectionVo;
import com.gd.coreservice.mapper.CollectionMapper;
import com.gd.coreservice.service.CollectionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author testjava
 * @since 2024-03-23
 */
@Service
public class CollectionServiceImpl extends ServiceImpl<CollectionMapper, Collection> implements CollectionService {


    @Override
    public List<CollectionVo> findCollection(String uid, String itemName) {

        List<CollectionVo> data = baseMapper.findCollection(uid,itemName);
        return data;
    }

    @Override
    public Integer getUidTotal(String uid, String itemName) {
        return baseMapper.getTotal(uid,itemName);
    }

    @Override
    public Boolean findMyCollection(String uid, String gid) {
        QueryWrapper<Collection> collectionQueryWrapper = new QueryWrapper<>();
        collectionQueryWrapper
                .eq("user_id",uid)
                .eq("item_id",gid);
        Integer i = baseMapper.selectCount(collectionQueryWrapper);
        if (0!=i){
            return true;
        }
        return false;
    }

    @Override
    public boolean insertCollection(String uid, String gid) {
        Collection collection  = new Collection();
        collection.setUserId(uid);
        collection.setItemId(gid);
        try {
            int insert = baseMapper.insert(collection);
            if (0!=insert)
                return true;
        }catch (Exception e){
        }
        return false;
    }
    //已测试  若删除成功 则会返回Integer类型的1
    @Override
    public Integer deleteCollection(String uid, String itemId) {
        QueryWrapper<Collection> collectionQueryWrapper = new QueryWrapper<>();
        collectionQueryWrapper
                .eq("user_id",uid)
                .eq("item_id",itemId);
        int delete = baseMapper.delete(collectionQueryWrapper);
        return delete;
    }
}
